﻿using System;
using System.Diagnostics;
using System.Windows;
using Microsoft.Phone.Controls;
using WeiboSdk;


namespace MyDeeWeibo
{
    public partial class MainPage : PhoneApplicationPage
    {

        #region ProgressIndicatorIsVisibleProperty
        public static readonly DependencyProperty ProgressIndicatorIsVisibleProperty =
            DependencyProperty.Register("ProgressIndicatorIsVisible",
            typeof(bool),
            typeof(MainPage),
            new PropertyMetadata(false));

        public bool ProgressIndicatorIsVisible
        {
            get { return (bool)GetValue(ProgressIndicatorIsVisibleProperty); }
            set { SetValue(ProgressIndicatorIsVisibleProperty, value); }
        }
        #endregion

        public bool canLogin = true;

        // Constructor
        public MainPage()
        {
            InitializeComponent();

            // 此处使用自己 AppKey 和 AppSecret，未经
            //审核的应用只支持用申请该Appkey的帐号来获取数据
            SdkData.AppKey = "";
            SdkData.AppSecret = "";
            App.AccessToken = "2.00f9Z5oBiv8NGE77dffd5a86lOOxDD";

            // 您app设置的重定向页,必须一致
            SdkData.RedirectUri = "http://weibo.com/61103972";
        }

        private void loginButton_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/PageViews/DeeTimeline.xaml", UriKind.Relative));
#if tmp            
            loginButton.Visibility = Visibility.Collapsed;
            username.Visibility = Visibility.Collapsed;
            password.Visibility = Visibility.Collapsed;

            SystemTray.ProgressIndicator = new ProgressIndicator();
            SystemTray.ProgressIndicator.Text = "数据传输中";

            Binding bindingData;
            bindingData = new Binding("ProgressIndicatorIsVisible");
            bindingData.Source = this;
            BindingOperations.SetBinding(SystemTray.ProgressIndicator, ProgressIndicator.IsVisibleProperty, bindingData);
            BindingOperations.SetBinding(SystemTray.ProgressIndicator, ProgressIndicator.IsIndeterminateProperty, bindingData);

            if (string.IsNullOrEmpty(SdkData.AppKey) || string.IsNullOrEmpty(SdkData.AppSecret))
            {
                Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    MessageBox.Show("请在中MainPage.xmal.cs的构造函数中设置自己的appkey、appkeysecret、RedirectUri.");
                });
                return;
            }

            AuthenticationView.OAuth2VerifyCompleted = (e1, e2, e3) => VerifyBack(e1, e2, e3);
            AuthenticationView.OBrowserCancelled = new EventHandler(cancleEvent);
            //其它通知事件...

            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                NavigationService.Navigate(new Uri("/WeiboSdk;component/PageViews/AuthenticationView.xaml"
                    , UriKind.Relative));
            });
#endif
        }

        private void VerifyBack(bool isSucess, SdkAuthError errCode, SdkAuth2Res response)
        {

            if (errCode.errCode == SdkErrCode.SUCCESS)
            {
                if (null != response)
                {
                    App.AccessToken = response.accesssToken;
                    App.RefleshToken = response.refleshToken;
                }

                Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    NavigationService.Navigate(new Uri("/PageViews/DeeTimeline.xaml", UriKind.Relative));
                });
            }
            else if (errCode.errCode == SdkErrCode.NET_UNUSUAL)
            {
                Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    MessageBox.Show("检查网络");
                });
            }
            else if (errCode.errCode == SdkErrCode.SERVER_ERR)
            {
                Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    MessageBox.Show("服务器返回错误，错误代码:" + errCode.specificCode);
                });
            }
            else
                Debug.WriteLine("Other Err.");

        }

        private void cancleEvent(object sender, EventArgs e)
        {
            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                NavigationService.GoBack();
            });
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {

            //客户端方式获取，需要服务器授权
            //ClientOAuth2_0.GetAccessToken("UserName", "PassWord", (e1, e2) => loginBack(e1,e2));
        }

        private void homeButton_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/PageViews/DeeTimeline.xaml", UriKind.Relative));
        }
    }
}